﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace QuailDashboard.View.Converters
{
    public class FilterIconConverter : IValueConverter
    {
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType.FullName == "System.Windows.Media.ImageSource" && value is bool && parameter is string)
            {
                // icon keys are defined in /Assets/Styles.xaml
                string key;
                var val = (bool) value;
                switch ((string) parameter)
                {
                    case "YSOD":
                        key = val ? "IconFailYsodYes" : "IconFailYsod";
                        break;
                    case "Fail":
                        key = val ? "IconFailYes" : "IconFail";
                        break;
                    case "Pass":
                        key = val ? "IconPassYes" : "IconPass";
                        break;
                    case "Warning":
                        key = val ? "IconWarningYes" : "IconWarning";
                        break;
                    case "AsyncPending":
                        key = val ? "IconAsyncPendingYes" : "IconAsyncPending";
                        break;
                    case "Pending":
                        key = val ? "IconPendingYes" : "IconPending";
                        break;
                    case "Timeout":
                        key = val ? "IconFailTimeoutYes" : "IconFailTimeout";
                        break;
                    case "Quail":
                        key = val ? "IconFailQuailYes" : "IconFailQuail";
                        break;
                    case "Sync":
                        key = val ? "IconFailSyncYes" : "IconFailSync";
                        break;
                    case "ThirdParty":
                        key = val ? "IconFailThirdPartyYes" : "IconFailThirdParty";
                        break;
                    case "Chrome":
                        key = "IconChrome";
                        break;
                    default:
                        key = "IconOther";
                        break;
                }

                return Application.Current.Resources[key];
            }

            return null;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
